package com.yao.club.service.impl;

import com.yao.club.entity.UserEntity;
import com.yao.club.entity.dto.Message;
import com.yao.club.entity.request.AddMessageRequest;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.mapper.UserMapper;
import com.yao.club.reposittory.MessageRepository;
import com.yao.club.service.MessageService;
import com.yao.club.utils.ResponseUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageRepository messageRepository;
    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseEntity<Boolean> addMessage(AddMessageRequest request) {
        Message message = new Message();
        message.setModule(request.getModule());
        message.setTitle(request.getTitle());
        message.setContent(request.getContent());
        message.setUrl(request.getUrl());
        message.setMemberId(request.getMemberId());
        message.setIfRead(request.getIfRead() != null ? request.getIfRead() : 0);
        message.setParam(request.getParam());
        message.setCreateTime(Instant.now());

        messageRepository.save(message);
        return ResponseUtils.success(true);
    }

    @Override
    public ResponseEntity<List<Message>> getLatestMessagesByModule(String username) {
        UserEntity currentUser = userMapper.select(username);
        if (currentUser == null) {
            return ResponseUtils.failed("用户不存在");
        }
        List<Message> messages = messageRepository.findLatestMessagesByModule(currentUser.member_id+"");
        return  ResponseUtils.success(messages);
    }
    @Override
    public ResponseEntity<List<Message>> readMessagesByModule(String username, int module) {
        UserEntity currentUser = userMapper.select(username);
        if (currentUser == null) {
            return ResponseUtils.failed("用户不存在");
        }

        // 查询当前用户指定模块的消息
        List<Message> messages = messageRepository.findByMemberIdAndModule(currentUser.getMember_id() + "", module);

        // 将未读消息标记为已读
        messages.stream()
                .filter(message -> message.getIfRead() == 0) // 筛选未读消息
                .forEach(message -> {
                    message.setIfRead(1); // 标记为已读
                    messageRepository.save(message); // 保存更新
                });

        return ResponseUtils.success(messages);
    }
    @Override
    public ResponseEntity<List<Message>> getMessagesByModule(String username, int module, int page, int size) {
        UserEntity currentUser = userMapper.select(username);
        if (currentUser == null) {
            return ResponseUtils.failed("用户不存在");
        }

        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createTime"));
        Page<Message> messages = messageRepository.findByMemberIdAndModule(currentUser.getMember_id() + "", module, pageable);

        return ResponseUtils.success(messages.getContent());
    }
}